package com.sk.weichat.ui.me;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.BottomSheetBehavior;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.sk.weichat.R;
import com.sk.weichat.audio_x.VoicePlayer;
import com.sk.weichat.bean.collection.CollectionEvery;
import com.sk.weichat.db.InternationalizationHelper;
import com.sk.weichat.ui.base.BaseActivity;
import com.sk.weichat.util.ViewPiexlUtil;
import com.sk.weichat.view.SelectionFrame;

import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

/**
 * Created by Administrator on 2017/10/20 0020.
 * 我的收藏
 */
public class MyCollectionActivity extends BaseActivity implements CollectionFragment.LoadListener {

    TextView tvEdit;
    private boolean isEdit = false;

    BottomSheetBehavior bottomSheetBehavior;

    CollectionFragment myCollectionFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_collection);
        initActionBar();
        setupBottomSheet();
        initView();
        myCollectionFragment = new CollectionFragment();
        myCollectionFragment.setListener(this);
        Bundle data = new Bundle();
        data.putBoolean("isItemDeleteAble", true);
        myCollectionFragment.setArguments(data);
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_content, myCollectionFragment).commit();
    }

    FrameLayout content;

    private void initView() {
        content = findViewById(R.id.fl_content);
        //批量删除
        findViewById(R.id.tv_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDeleteMsgDialog(new SelectionFrame.OnSelectionFrameClickListener() {

                    @Override
                    public void cancelClick() {

                    }

                    @Override
                    public void confirmClick() {
                        List<CollectionEvery> list = myCollectionFragment.getData();
                        for (int i = 0; i < list.size(); i++) {
                            CollectionEvery item = list.get(i);
                            item.isEdit = false;
                            if (item.isChecked) {
                                myCollectionFragment.deleteCollection(item);
                            }
                        }
                        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
                        myCollectionFragment.notifiDataSetChange();

                    }
                });

            }
        });
    }


    /* 操作事件 */
    private void showDeleteMsgDialog(SelectionFrame.OnSelectionFrameClickListener onSelectionFrameClickListener) {
        SelectionFrame dialog = new SelectionFrame(mContext);
        dialog.setSomething(null, mContext.getString(R.string.delete_prompt), onSelectionFrameClickListener);
        dialog.show();
    }

    private void initActionBar() {
        getSupportActionBar().hide();
        findViewById(R.id.iv_title_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopVoiceOrVideo();
            }
        });

        tvEdit = findViewById(R.id.tv_title_right);
        tvEdit.setEnabled(false);
        tvEdit.setTextColor(getResources().getColor(R.color.deep_purple));
        tvEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (myCollectionFragment.getData().size() == 0) {
                    return;
                }
                isEdit = !isEdit;
                myCollectionFragment.setSelectAble(isEdit);

                if (isEdit) {
                    tvEdit.setText("取消");
                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                } else {
                    tvEdit.setText("编辑");
                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
                }
            }
        });

        TextView tvTitle = findViewById(R.id.tv_title_center);
        //  tvTitle.setText(InternationalizationHelper.getString("JX_MyCollection"));
        tvTitle.setText(InternationalizationHelper.getString("JX_MyCollection"));

    }

    private void stopVoiceOrVideo() {
        VoicePlayer.instance().stop();
        JCVideoPlayer.releaseAllVideos();
        finish();
    }

    @Override
    public void onBackPressed() {
        stopVoiceOrVideo();
        super.onBackPressed();
    }

    private void setupBottomSheet() {
        bottomSheetBehavior = BottomSheetBehavior.from(findViewById(R.id.tv_delete));
        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
        bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View view, int i) {

            }

            @Override
            public void onSlide(@NonNull View view, float v) {

                ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) content.getLayoutParams();

                int marginBottom = ViewPiexlUtil.dp2px(mContext, 60);

                lp.bottomMargin = (int) (marginBottom + marginBottom * v);

                content.setLayoutParams(lp);
                if (Float.isNaN(v)) {
                    //显示
                    //Log.e("lzs", "显示");
                    lp.bottomMargin = ViewPiexlUtil.dp2px(mContext, 60);
                    content.setLayoutParams(lp);
                } else if (v == -1.0f) {
                    //隐藏

                    isEdit = false;
                    tvEdit.setText("编辑");
                    myCollectionFragment.setSelectAble(isEdit);
                   // Log.e("lzs", "隐藏");
                }
            }
        });
    }


    @Override
    public void onLoading() {

    }

    @Override
    public void onLoadFinish() {
        if (myCollectionFragment.getData().size() > 0) {
            tvEdit.setText("编辑");
            tvEdit.setEnabled(true);

        } else {
            tvEdit.setText("");
            tvEdit.setEnabled(false);
        }
        if (bottomSheetBehavior != null) {
            bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
        }
    }
}
